package com.deyou.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.deyou.R;
import com.deyou.util.GlideImageLoader;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by yangyuancheng on 2017/6/16.
 */

public class FeedBackGVItemAdapter extends BaseAdapter {
    private Context mContext;
    private LayoutInflater mInflater;
    private ArrayList<String> imgs = new ArrayList<>();

    public void setImgs(ArrayList<String> imgs) {
        this.imgs = imgs;
        notifyDataSetChanged();
    }

    public FeedBackGVItemAdapter(Context mContext) {
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        int size = 0;
        if (imgs == null)
            size = 0;
        if (imgs.size() >= 9)
            size = 9;
        else
            size = imgs.size() + 1;
        return size;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (null == convertView) {
            convertView = mInflater.inflate(R.layout.gv_img_item_layout, parent, false);
            holder = new ViewHolder(convertView);
            holder.imgGvItem = (ImageView) convertView.findViewById(R.id.img_gv_item);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (null == imgs || imgs.size() == 0){
            new GlideImageLoader().displayImage(mContext, R.drawable.bg_gridview_add, holder.imgGvItem);
        }else if (imgs.size() > 0 && imgs.size() < 9){
            if (position == imgs.size())
                new GlideImageLoader().displayImage(mContext, R.drawable.bg_gridview_add, holder.imgGvItem);
            else
                new GlideImageLoader().displayImage(mContext, imgs.get(position), holder.imgGvItem);
        } else if (imgs.size() >= 9){
            new GlideImageLoader().displayImage(mContext, imgs.get(position), holder.imgGvItem);
        }
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.img_gv_item)
        ImageView imgGvItem;

        ViewHolder(View view) {
            AutoUtils.auto(view);
            ButterKnife.bind(this, view);
        }
    }
}
